<?php
  class PostController extends Controller{


    public function IndexAction(){
      $pm = new PostMapper();
      $postList = $pm->getNPosts(0,5);
      $ids = ArrayAndObjectParser::ObjectPropiertiesWithJointName('authorId',$postList);

      $authorNames = UserMapper::getManyUsersNameByIds($ids);
      $this->view->authorNames = $authorNames;
      $this->view->posts = $postList;
      $this->view->set_tpl('PostsIndex');
    }

    public function WritePostAction(){
      $this->view->set_tpl('WritePost');
    }

    public function CreatePostAction(){
      $pm = new PostMapper();
      $tm = new TagMapper();
      $currentUser = UserMapper::getCurrentUser();

      if (!$currentUser->get_isGuest()){
        $post = new PostModel();
        $this->params['postHeader'] = htmlspecialchars($this->params['postHeader']);
        $this->params['postBody'] = UserInputValidator::strip_tags_attributes($this->params['postBody'], array('<a>','<p>','<strong>','<img>','<h3>'));
        $this->params['tags'] = htmlspecialchars($this->params['tags']);
        $post->set_header($this->params['postHeader'])
             ->set_body($this->params['postBody'])
             ->set_authorId($currentUser->get_id());
        $newId = $pm->save($post);
        $tm->bindTags($this->params['tags'],$newId);
        $this->IndexAction();
      }else{
        $this->view->set_folder('Error');
        $this->view->set_tpl('WrongUser');
      }
    }

    public function UpdatePostAction(){
      $cUser = UserMapper::getCurrentUser();
      $this->view->set_tpl('WrongUser');
      
      if($cUser->get_isGuest()){
        $this->view->set_folder('Error');
        return false;
      }
      $pm = new PostMapper();
      $tm = new TagMapper(); 
      $cPost = $pm->getPostById($this->params['postid']);
      if ($cUser->get_id()!==$cPost->get_authorId()) {
        $this->view->set_folder('Error');
        return false;
      }
      $post = new PostModel();
      $this->params['postHeader'] = htmlspecialchars($this->params['postHeader']);
      $this->params['postBody'] = UserInputValidator::strip_tags_attributes($this->params['postBody'], array('<a>','<p>','<strong>','<img>','<h3>'));
      $this->params['tags'] = htmlspecialchars($this->params['tags']);
      $post->set_header($this->params['postHeader'])
           ->set_body($this->params['postBody'])
           ->set_id($cPost->get_id());
      $pm->update($post);
      $tm->bindTags($this->params['tags'],$cPost->get_id());
      $this->IndexAction();

    }
    
    public function EditAction(){
      $cUser = UserMapper::getCurrentUser();
      $this->view->set_tpl('WrongUser');
      
      if($cUser->get_isGuest()){
        $this->view->set_folder('Error');
        return false;
      }
      $pm = new PostMapper();
      $cPost = $pm->getPostById($this->params['postid']);
      // if ($cUser->get_id()!==intval($cPost->get_authorId())) {
      //   $this->view->set_folder('Error');
      //   return false;
      // }
      $this->view->post = $cPost;
      $this->view->set_tpl('WritePost');
    }
  }
?>